package cn.qzuie.smart.module.dorm.service.energyusage;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.energyusage.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.energyusage.EnergyUsageDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.energyusage.EnergyUsageMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 能源使用 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EnergyUsageServiceImpl implements EnergyUsageService {

    @Resource
    private EnergyUsageMapper energyUsageMapper;

    @Override
    public Long createEnergyUsage(EnergyUsageSaveReqVO createReqVO) {
        // 插入
        EnergyUsageDO energyUsage = BeanUtils.toBean(createReqVO, EnergyUsageDO.class);
        energyUsageMapper.insert(energyUsage);

        // 返回
        return energyUsage.getId();
    }

    @Override
    public void updateEnergyUsage(EnergyUsageSaveReqVO updateReqVO) {
        // 校验存在
        validateEnergyUsageExists(updateReqVO.getId());
        // 更新
        EnergyUsageDO updateObj = BeanUtils.toBean(updateReqVO, EnergyUsageDO.class);
        energyUsageMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyUsage(Long id) {
        // 校验存在
        validateEnergyUsageExists(id);
        // 删除
        energyUsageMapper.deleteById(id);
    }

    @Override
        public void deleteEnergyUsageListByIds(List<Long> ids) {
        // 删除
        energyUsageMapper.deleteByIds(ids);
        }


    private void validateEnergyUsageExists(Long id) {
        if (energyUsageMapper.selectById(id) == null) {
            throw exception(ENERGY_USAGE_NOT_EXISTS);
        }
    }

    @Override
    public EnergyUsageDO getEnergyUsage(Long id) {
        return energyUsageMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyUsageDO> getEnergyUsagePage(EnergyUsagePageReqVO pageReqVO) {
        return energyUsageMapper.selectPage(pageReqVO);
    }

}